package com.decade.controller;

import cn.hutool.core.lang.Dict;
import com.decade.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gzk
 * @date 2021/48/11 15:48
 */
@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private StockService stockService;

    @GetMapping("/getStockByGoodsId")
    public Dict getStockByGoodsId() {
        return Dict.create().set("stock", stockService.getStockByGoodsId(1L));
    }

    /**
     * 模拟秒杀
     * @param
     * @return
     */
    @GetMapping("/createOrderTest")
    public Dict createOrderTest() {
        if (!stockService.decrementProductStore(1L, 1)) {
            return Dict.create().set("info", "库存不足");
        }
        // 创建订单
        return Dict.create().set("info", "创建订单成功");
    }
}
